package cn.boge.hrm.service.impl;

import cn.boge.hrm.client.AuthServiceClient;
import cn.boge.hrm.domain.LoginUser;
import cn.boge.hrm.domain.VipBase;
import cn.boge.hrm.domain.VipUser;
import cn.boge.hrm.dto.LoginUserDto;
import cn.boge.hrm.dto.SMSCodeDTO;
import cn.boge.hrm.dto.UserRegisterDTO;
import cn.boge.hrm.exception.Assert;
import cn.boge.hrm.exception.BusinessException;
import cn.boge.hrm.mapper.VipBaseMapper;
import cn.boge.hrm.mapper.VipUserMapper;
import cn.boge.hrm.service.IVipUserService;
import cn.boge.hrm.util.AjaxResult;
import cn.boge.hrm.util.BitStatesUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author boge
 * @since 2022-04-13
 */
@Service
public class VipUserServiceImpl extends ServiceImpl<VipUserMapper, VipUser> implements IVipUserService {
    @Autowired
    private VipBaseMapper vipBaseMapper;
    @Autowired
    private AuthServiceClient authServiceClient;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    /**
     * 参数校验
     * 条件校验 判断验证码 手机号查重
     * 用户中心集成fegin
     * 保存login 返回id
     * 保存vip_user 设置loginid
     * 保存vip_base
     * @param dto
     */
    @Override
    @GlobalTransactional
    public void register(UserRegisterDTO dto) {
        //* 参数校验
        Assert.assertPhone(dto.getMobile(),"无效的手机号");
        //* 条件校验 判断验证码
        SMSCodeDTO smsCodeDTO = (SMSCodeDTO)redisTemplate.opsForValue().get("sms:"+dto.getMobile());
        Assert.assertNotNull(smsCodeDTO,"无效的验证码或过期");
        if (!smsCodeDTO.getSmsCode().equals(dto.getSmsCode())){
            throw new BusinessException("验证码错误");
        }
        //手机号查重
        EntityWrapper<VipUser> wrapper = new EntityWrapper<>();
        wrapper.eq("phone",dto.getMobile());
        VipUser vipUserFromDB = selectOne(wrapper);
        Assert.assertNull(vipUserFromDB,"手机号已经注册，请直接登录");
        //* 保存login 返回id
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(dto.getMobile());
        loginUser.setPassword(dto.getPassword());
        loginUser.setType(LoginUser.USER_TYPE_WEB);
        AjaxResult result = authServiceClient.add(new LoginUserDto(loginUser, null));
        Assert.assertTrue(result.isSuccess(),"登录信息保存失败");
        Assert.assertNotNull(result.getResultObj(),"登录信息保存失败");
        Long loginid = Long.valueOf(result.getResultObj().toString());
        //* 保存vip_user 设置loginid
        VipUser vipUser = new VipUser();
        vipUser.setCreateTime(new Date().getTime());
        vipUser.setPhone(dto.getMobile());
        vipUser.setNickName(dto.getMobile());
        vipUser.setLoginId(loginid);
        long bitState = BitStatesUtils.addState(0l, BitStatesUtils.OP_REGISTED);
        bitState = BitStatesUtils.addState(bitState, BitStatesUtils.OP_ACTIVED);
        bitState = BitStatesUtils.addState(bitState, BitStatesUtils.OP_AUTHED_PHONE);
        vipUser.setBitState(bitState);
        insert(vipUser);
        //* 保存vip_base
        VipBase vipBase = new VipBase();
        vipBase.setRegTime(vipUser.getCreateTime());
        vipBase.setSsoId(vipUser.getId());
        //注册渠道
        vipBase.setRegChannel(dto.getRegChannel());
        vipBase.setRegTime(vipUser.getCreateTime());
        vipBaseMapper.insert(vipBase);
        //int a = 1/0;
    }
}
